#include "precomp.h"

namespace Tmpl {

float Vec2f::Length() const
{
	return sqrtf(x*x + y*y);
}

int Vec2i::Length() const
{
	return (int)sqrtf((float)(x*x + y*y));
}

float Vec3f::Length() const
{
	return sqrtf(x*x + y*y + z*z);
}

int Vec3i::Length() const
{
	return (int)sqrtf((float)(x*x + y*y + z*z));
}

void Matrix3x3f::Rotate(float rot)
{
	Identity();
	const float sz = sinf(rot * (3.141593f/180.0f));
	const float cz = cosf(rot * (3.141593f/180.0f));
	cell[0] = cz, cell[1] = sz, cell[3] = -sz, cell[4] = cz;
}

void Matrix4x4f::SetRotateX(float rotX)
{
	Identity();
	const float sx = sinf(rotX * (3.141593f/180.0f));
	const float cx = cosf(rotX * (3.141593f/180.0f));
	cell[5] = cx, cell[6] = sx, cell[9] = -sx, cell[10] = cx;
}

void Matrix4x4f::SetRotateY(float rotY)
{
	Identity();
	const float sy = sinf(rotY * (3.141593f/180.0f));
	const float cy = cosf(rotY * (3.141593f/180.0f));
	cell[0] = cy, cell[2] = -sy, cell[8] = sy, cell[10] = cy;
}

void Matrix4x4f::SetRotateZ(float rotZ)
{
	Identity();
	const float sz = sinf(rotZ * (3.141593f/180.0f));
	const float cz = cosf(rotZ * (3.141593f/180.0f));
	cell[0] = cz, cell[1] = sz, cell[4] = -sz, cell[5] = cz;
}

void Matrix4x4f::RotateX(float rotX)
{
	Matrix4x4 m;
	const float sx = sinf(rotX * (3.141593f/180.0f));
	const float cx = cosf(rotX * (3.141593f/180.0f));
	m.cell[5] = cx, m.cell[6] = sx, m.cell[9] = -sx, m.cell[10] = cx;
	Concatenate(m);
}

void Matrix4x4f::RotateY(float rotY)
{
	Matrix4x4 m;
	const float sy = sinf(rotY * (3.141593f/180.0f));
	const float cy = cosf(rotY * (3.141593f/180.0f));
	m.cell[0] = cy, m.cell[2] = -sy, m.cell[8] = sy, m.cell[10] = cy;
	Concatenate(m);
}

void Matrix4x4f::RotateZ(float rotZ)
{
	Matrix4x4 m;
	const float sz = sinf(rotZ * (3.141593f/180.0f));
	const float cz = cosf(rotZ * (3.141593f/180.0f));
	m.cell[0] = cz, m.cell[1] = sz, m.cell[4] = -sz, m.cell[5] = cz;
	Concatenate(m);
}

void Matrix4x4f::SetPerspective(float fov, float aspect, float zNear, float zFar)
{
	for (int i=0; i<16; ++i) cell[i] = 0;
	const float f = 1.0f / tan( fov * 3.141593f / 360.0f ); //1.0f / tan( fov * 0.5f );
	cell[0] = f / aspect;
	cell[5] = f;
	cell[10] = (zFar + zNear) / (zNear - zFar);
	cell[11] = -1.0f;
	cell[14] = (2.0f * zFar * zNear) / (zNear - zFar);

	/*
   GLdouble xmin, xmax, ymin, ymax;
   ymax = zNear * tan(fovy * M_PI / 360.0);
   ymin = -ymax;
   xmin = ymin * aspect;
   xmax = ymax * aspect;
   */


  // glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);


}

}; // namespace Tmpl